<?php

if ($action == "add") { 

	$insertSQL = sprintf("INSERT INTO courses (name, aid, did, jiu_course_id, proposal_complete, course_complete, archive) VALUES (%s, %s, %s, %s, %s, %s, %s);",
				GetSQLValueString($_POST['name'], "text"),
				GetSQLValueString($_POST['aid'], "int"),
				GetSQLValueString($_POST['did'], "int"),
				GetSQLValueString($_POST['jiu_course_id'], "text"),
				GetSQLValueString("N", "text"),
				GetSQLValueString("N", "text"),
				GetSQLValueString("N", "text")
			);
  						
		mysql_select_db($database, $connection);
  		$result = mysql_query($insertSQL, $connection) or die(mysql_error());
		//echo "<p>".$insertSQL."<br>";
		
		$query_cid = "SELECT id FROM courses ORDER BY id DESC LIMIT 1";
		$cid = mysql_query($query_cid, $connection) or die(mysql_error());
		$row_cid = mysql_fetch_assoc($cid);
		
		// Add specified number modules to the 'modules' table
		$a = $_POST['modules'];
		for($i=1; $i<=$a; $i=$i+1) {
			$insertModules = sprintf("INSERT INTO modules (cid, mid) VALUES ('%s','%s');", $row_cid['id'], $i);
			mysql_select_db($database, $connection);
  			$result = mysql_query($insertModules, $connection) or die(mysql_error());
			
			// Add two default assignments for each module in the specified course
			$insertAssignments  = sprintf("INSERT INTO assignments (cid , mid, assign_num, assign_title) VALUES ('%s','%s','1','Discussion - [Title] '); ", $row_cid['id'], $i);
  			$result = mysql_query($insertAssignments, $connection) or die(mysql_error());
			
			$insertAssignments = sprintf("INSERT INTO assignments (cid , mid, assign_num, assign_title) VALUES ('%s','%s','2','Course Project - [Title] '); ", $row_cid['id'], $i);
  			$result = mysql_query($insertAssignments, $connection) or die(mysql_error());			
			
			// Add two default themes for each module in the specified course
			$insertThemes = sprintf("INSERT INTO themes (cid, mid, theme_num) VALUES ('%s','%s','%s');", $row_cid['id'], $i, "1");
  			$result = mysql_query($insertThemes, $connection) or die(mysql_error());
			
			$insertThemes = sprintf("INSERT INTO themes (cid, mid, theme_num) VALUES ('%s','%s','%s');", $row_cid['id'], $i, "2");
  			$result = mysql_query($insertThemes, $connection) or die(mysql_error());
			}
		
		$insertGoTo = "../index.php?section=".$section."&action=view&msg=1";
		header(sprintf("Location: %s", $insertGoTo));
	}

	if ($action == "edit") {
	$updateSQL = sprintf("UPDATE courses 
			SET 
			name=%s,
			aid=%s,
			did=%s,
			jiu_course_id=%s
			WHERE id=%s", 
				GetSQLValueString($_POST['name'], "text"),
				GetSQLValueString($_POST['aid'], "int"),
				GetSQLValueString($_POST['did'], "int"),
				GetSQLValueString($_POST['jiu_course_id'], "text"),
				GetSQLValueString($id, "int")
			);
  						
		mysql_select_db($database, $connection);
  		$result1 = mysql_query($updateSQL, $connection) or die(mysql_error());
		//echo $updateSQL;
		
		$updateGoTo = "../index.php?section=".$section."&action=view&msg=4";
		header(sprintf("Location: %s", $updateGoTo));

	}

	if ($action == "archive") { 
		$query_ch = sprintf("SELECT * FROM courses WHERE id='%s'", $id);
		$ch = mysql_query($query_ch, $connection) or die(mysql_error());
		$row_ch = mysql_fetch_assoc($ch);
		if ($row_ch['archive'] == "Y") $archive = "N"; else $archive = "Y"; 
		$updateSQL = sprintf("UPDATE courses 
			SET 
			archive=%s
			WHERE id=%s", 
				GetSQLValueString($archive, "text"),
				GetSQLValueString($id, "int")
			);
  						
		mysql_select_db($database, $connection);
  		$result1 = mysql_query($updateSQL, $connection) or die(mysql_error());
		//echo $updateSQL;
		
		$updateGoTo = "../index.php?section=courses&action=view&msg=4";
		header(sprintf("Location: %s", $updateGoTo));
}

	if ($action == "change") { 
		$query_ch = sprintf("SELECT id,proposal_complete,course_complete FROM courses WHERE id='%s'", $id);
		$ch = mysql_query($query_ch, $connection) or die(mysql_error());
		$row_ch = mysql_fetch_assoc($ch);
		
		if ($go == "p_complete") {
			if ($row_ch['proposal_complete'] == "Y") $proposal_complete = "N"; 
			if ($row_ch['proposal_complete'] == "N") $proposal_complete = "Y";

			$updateSQL = sprintf("UPDATE courses 
			SET 
			proposal_complete=%s
			WHERE id=%s", 
				GetSQLValueString($proposal_complete, "text"),
				GetSQLValueString($id, "int")
			);
		
		}
		if ($go == "c_complete") {
			if ($row_ch['course_complete'] == "Y") $course_complete = "N";
			if ($row_ch['course_complete'] == "N") $course_complete = "Y"; 
			
			$updateSQL = sprintf("UPDATE courses 
			SET 
			course_complete=%s
			WHERE id=%s", 
				GetSQLValueString($course_complete, "text"),
				GetSQLValueString($id, "int")
			);
		}
		
		mysql_select_db($database, $connection);
  		$result1 = mysql_query($updateSQL, $connection) or die(mysql_error());
		//echo $updateSQL;
		
		$updateGoTo = "../index.php?section=dashboard";
		header(sprintf("Location: %s", $updateGoTo));
	}

?>